package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HomeGetBuilding;
import com.sander.guardmanagement.http.HomeGetRoom;
import com.sander.guardmanagement.http.HomeGetSubCommunity;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.model.City;
import com.sander.guardmanagement.model.District;
import com.sander.guardmanagement.presenter.OnItemOnclickUniteListener;
import com.sander.guardmanagement.response.ResponseHomeGetBuilding;
import com.sander.guardmanagement.response.ResponseHomeGetBuildingData;
import com.sander.guardmanagement.response.ResponseHomeGetCityData;
import com.sander.guardmanagement.response.ResponseHomeGetCommunityData;
import com.sander.guardmanagement.response.ResponseHomeGetRoom;
import com.sander.guardmanagement.response.ResponseHomeGetRoomData;
import com.sander.guardmanagement.response.ResponseHomeGetSubCommunity;
import com.sander.guardmanagement.response.ResponseHomeGetSubCommunityData;
import com.sander.guardmanagement.ui.model.DistrictBoxModel;
import com.sander.guardmanagement.ui.model.SelectedBoxModel;
import com.sander.guardmanagement.ui.view.DistrictBoxAdapter;
import com.sander.guardmanagement.ui.view.DistrictListAdapter;
import com.sander.guardmanagement.widgets.ActionSheetDialog;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

public class DistrictSelectedUnitActivity extends AppCompatActivity {
    private ActionSheetDialog mActionSheetDialog;
    public  ResponseHomeGetCommunityData communityData;
    public List<ResponseHomeGetSubCommunityData> boxList = new ArrayList<>();

    public String selected_title = "";

    public SelectedBoxModel selectedBoxModel = new SelectedBoxModel();

    DistrictBoxAdapter boxAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district_selected_unit);
        initCallBack();
        Intent intent = this.getIntent();
        communityData=(ResponseHomeGetCommunityData) intent.getSerializableExtra("district");
        selectedBoxModel.setCommunityData(communityData);

        TextView textView = (TextView) findViewById(R.id.toolbarTitle);
        textView.setText(communityData.getCommunity());

        selected_title = communityData.getCommunity();
        initData();
        initView();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }
    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData(){

        getbuilding();
//        HomeGetSubCommunity homeGetSubCommunity = new HomeGetSubCommunity();
//        homeGetSubCommunity.community_id = communityData.getId();
//        OkHttpHelper.getinstance().post(homeGetSubCommunity.requestUrl(), homeGetSubCommunity.getValueOfClas(), new BaseCallback<ResponseHomeGetSubCommunity>() {
//            @Override
//            public void onRequestBefore() {
//
//            }
//
//            @Override
//            public void onFailure(Request request, Exception e) {
//
//            }
//
//            @Override
//            public void onSuccess(Response response, ResponseHomeGetSubCommunity o) {
//                Log.d("sander",o.getMsg());
//                if (o.getCode() == 0){
//                    List<ResponseHomeGetSubCommunityData> list = o.getData();
//                    for (ResponseHomeGetSubCommunityData data : list){
//                        getsubCommunity(data);
//                    }
//                }
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, Exception e) {
//
//            }
//        });

    }

    public void getbuilding(){

        HomeGetBuilding building = new HomeGetBuilding();
        building.community_id = communityData.getId() + "";
        OkHttpHelper.getinstance().post(building.requestUrl(), building.getValueOfClas(), new BaseCallback<ResponseHomeGetBuilding>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseHomeGetBuilding o) {
                Log.d("sander",o.getMsg());
                if (o.getCode() == 0){
                    List<ResponseHomeGetSubCommunityData> list = o.getData();
                    boxList = list;
                    updateListData();
                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });


    }
    public void updateListData(){
        if (boxAdapter != null){
            boxAdapter.boxList = boxList;
            boxAdapter.notifyDataSetChanged();
        }
//        initView();

    }


    private void initView(){

        ListView listView = (ListView)findViewById(R.id.listView);

        List<DistrictBoxModel> list = new ArrayList<>();
        DistrictBoxModel model = new DistrictBoxModel();
        list.add(model);

        boxAdapter = new DistrictBoxAdapter(getApplicationContext(),boxList);

        listView.setAdapter(boxAdapter);

        boxAdapter.setClickListener(new OnItemOnclickUniteListener() {
            @Override
            public void onClickItem(int index,int section) {
                Log.d("sander","click listener at index " + index);
                ResponseHomeGetSubCommunityData boxModel = boxList.get(section);
                ResponseHomeGetBuildingData buildingData = boxModel.getBuilding_list().get(index);
                selectedBoxModel.setSubCommunityData(boxModel);
                selectedBoxModel.setBuildingData(buildingData);

                selected_title = communityData.getCommunity();
                selected_title += boxModel.getSub_community_name() + buildingData.building_name;
                onLoadActionSheetData(buildingData);

            }
        });



    }

    public void onLoadActionSheetData(ResponseHomeGetBuildingData buildingData){
        HomeGetRoom homeGetRoom = new HomeGetRoom();
        homeGetRoom.building_id = buildingData.getId() + "";
        OkHttpHelper.getinstance().post(homeGetRoom.requestUrl(), homeGetRoom.getValueOfClas(), new BaseCallback<ResponseHomeGetRoom>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseHomeGetRoom o) {
                if (o.getCode() == 0){
                    List<ResponseHomeGetRoomData> list = o.getData();
                    showActionSheet(list);

                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }

    public void showActionSheet(final List<ResponseHomeGetRoomData> list){
        String str_list[] = new String[list.size()];
        for (int i = 0;i < list.size(); i ++){
            ResponseHomeGetRoomData data = list.get(i);
            str_list[i] = data.room_name;
        }
        if (mActionSheetDialog == null) {
            mActionSheetDialog = new ActionSheetDialog(DistrictSelectedUnitActivity.this);

        }
        mActionSheetDialog.setMenuListener(new ActionSheetDialog.MenuListener() {
            @Override
            public void onItemSelected(int position, String item) {
                Log.d("sander","--->  " + list.get(position).getBuilding_id() + "  "  + list.get(position).getId());
                selectedBoxModel.setRoomData(list.get(position));
                onClickItemDistrict();
            }

            @Override
            public void onCancel() {
                Toast.makeText(DistrictSelectedUnitActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
            }
        });
        mActionSheetDialog.loadList(str_list);
        mActionSheetDialog.loadActionSheetTitle(selected_title);
        mActionSheetDialog.show();

    }

    public void onClickItemDistrict(){

        Intent intent = new Intent(DistrictSelectedUnitActivity.this,SupplementaryInformationActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("box",selectedBoxModel);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
